﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TSV.Library.Common;
using System.Data.Common;
using System.Web.UI.WebControls;

namespace TSV.Models
{
    public class OrdersPartnerDao:BaseDao
    {
        public List<sp_GetOrderListResult> GetList(string text, int? categoryID, DateTime? fromDate, DateTime? toDate)
        {
            return dbContext.sp_GetOrderList(text, categoryID, fromDate, toDate).ToList<sp_GetOrderListResult>();
        }

        public List<ListItem> GetListStatus()
        {
            List<ListItem> list = new List<ListItem>();
            list.Add(new ListItem("Thành Công","1"));
            list.Add(new ListItem("Trả Lại", "2"));
            return list;
        }

        public OrdersPartner GetByID(int id)
        {
            return dbContext.OrdersPartners.Where(q => q.ID == id).FirstOrDefault();
        }

        public Message Insert(OrdersPartner objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    objUI.Status = 1;
                    dbContext.OrdersPartners.InsertOnSubmit(objUI);
                    dbContext.SubmitChanges();
                    // Show success message
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Đơn hàng '" + objUI.ID + "'", "thêm");
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }

        public Message Update(OrdersPartner objUI)
        {
            Message msg = null;
            try
            {
                OrdersPartner objDb = GetByID(objUI.ID);
                if (objDb != null)
                {
                    objDb.PartnerID = objUI.PartnerID;
                    objDb.Price = objUI.Price;
                    objDb.PriceDiscount = objUI.PriceDiscount;
                    objDb.Quality = objUI.Quality;
                    objDb.Total = objUI.Total;
                    objDb.Discount = objUI.Discount;
                    objDb.Status = objUI.Status;
                    dbContext.SubmitChanges();
                    // Show success message
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Đơn hàng '" + objUI.ID + "'", "cập nhật");
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }

        /// <summary>
        /// DeleteList
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public Message DeleteList(string ids)
        {
            Message msg = null;
            DbTransaction trans = null;
            try
            {
                dbContext.Connection.Open();
                trans = dbContext.Connection.BeginTransaction();
                dbContext.Transaction = trans;

                if (!string.IsNullOrEmpty(ids))
                {
                    bool canDelete = true;
                    ids = ids.TrimEnd(',');
                    int collaboratorID = 0;

                    string[] idArr = ids.Split(',');
                    int total = idArr.Count();
                    foreach (string id in idArr)
                    {
                        bool isValid = Int32.TryParse(id, out collaboratorID);
                        OrdersPartner partner = GetByID(collaboratorID);
                        if (partner != null)
                        {
                            bool isSuccess = Delete(partner);
                            if (!isSuccess)
                            {
                                canDelete = false;
                                // Show succes message
                                msg = new Message(MessageConstants.E0006, MessageType.Error, "xóa", "đối tác này");
                                break;
                            }
                        }
                        else
                        {
                            total--;
                        }
                    }

                    if (canDelete)
                    {
                        // Show succes message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, total.ToString() + " đối tác", "xóa");
                        trans.Commit();
                    }
                    else
                    {
                        trans.Rollback();
                    }
                }
            }
            catch (Exception)
            {
                if (trans != null) trans.Rollback();
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="objUI"></param>
        private bool Delete(OrdersPartner objUI)
        {
            bool result = true;
            if (objUI != null)
            {
                // Get current group in dbContext
                OrdersPartner objDb = GetByID(objUI.ID);
                if (objDb != null)
                {
                    dbContext.OrdersPartners.DeleteOnSubmit(objDb);
                    dbContext.SubmitChanges();
                }
                else
                {
                    result = false;
                }
            }
            return result;
        }
    }
}